package com.netted.operationassistant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.android.volley.VolleyError;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.netted.operationassistant.R;
import com.netted.operationassistant.constant.Constant;
import com.netted.operationassistant.http.HttpRequest;
import com.netted.operationassistant.sharedPreferences.SharedPreferencesHelper;


import java.util.HashMap;
import java.util.Map;

/**
 * A login screen that offers login via email/password.
 */
public class LoginActivity extends BaseActivity {


    // UI references.
    private EditText accountView;
    private EditText mPasswordView;
    private Button mEmailSignInButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        // Set up the login form.
        accountView = (EditText) findViewById(R.id.account);

        mPasswordView = (EditText) findViewById(R.id.password);
        mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
                if (id == EditorInfo.IME_ACTION_DONE || id == EditorInfo.IME_NULL) {
                    login();
                    return true;
                }
                return false;
            }
        });

        mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
        mEmailSignInButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });
    }



    public void login(){
        String account = accountView.getText()+"";
        String password = mPasswordView.getText() + "";
        if (account == null || account.equals("null") || "".equals(account.replace(" ",""))){
            ToastUtils.showShort("请填写完整账号");
            return;
        }

        if (password == null || password.equals("null") || "".equals(password.replace(" ",""))){
            ToastUtils.showShort("请填写正确密码");
            return;
        }


        Map<String,String> params = new HashMap<>();
        params.put("account",account);
        params.put("password",password);
        request.post(Constant.HOST+"/ws/opt/adminLogin",params, "post", new HttpRequest.Callback() {
            @Override
            public void onSuccess(String response) {
                LogUtils.file(1,"URL:"+Constant.HOST+"/ws/opt/adminLogin\n response:"+response);
                JSONObject json = JSONObject.parseObject(response);

                if (json.getString("flag").equals("success")){
                    String data = json.getString("data");
                    if (StringUtils.isEmpty(data)){
                        ToastUtils.showShort("服务器缺少参数返回");
                        return;
                    }
                    JSONObject user = JSONObject.parseObject(data);
                    Intent intent = new Intent(LoginActivity.this,AdminMenuActivity.class);
                    intent.putExtra("account",user.getString("account"));
                    intent.putExtra("password",user.getString("password"));
                    intent.putExtra("userId",user.getString("id"));
                    playSound(LoginActivity.this,R.raw.s8);
                    startActivity(intent);
                    ActivityUtils.finishActivity(LoginActivity.this);

                }else{
                    ToastUtils.showShort(json.getString("message"));
                }
            }

            @Override
            public void onError(VolleyError error) {
                ToastUtils.showShort(JSONObject.toJSONString(error));
            }
        });

    }

}

